﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;
using System.Windows.Media.Media3D;

namespace TestDxfDocument
{

    class _Tapswiththehollowtodeposittheswarf
    {
        public static void Tapswiththehollowtodeposittheswarf(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double d = Convert.ToDouble(data["td"]);
            double d1 = Convert.ToDouble(data["dconms"]);
            double p = Convert.ToDouble(data["tp"]);
            double L = Convert.ToDouble(data["oal"]);
            double l = Convert.ToDouble(data["thl"]);
            double a = Convert.ToDouble(data["drvsc"]);
            double l2 = Convert.ToDouble(data["drvl"]);
            double r = Convert.ToDouble(data["re"]);
            double l5= Convert.ToDouble(data["tcl"])*p;
            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + l5, v1.Y - 0.06 * d);
            Vector2 v3 = new Vector2(v2.X + 0.67 * l, v2.Y);
            Vector2 v4 = new Vector2(v3.X + 0.157*d, v3.Y + ((d-d1)*0.5));
            Vector2 v5 = new Vector2(v4.X + 0.234 * L, v4.Y);
            Vector2 v6 = new Vector2(v5.X + 0.103*L, v5.Y);
            

            //作图
            LwPolyline line1 = new LwPolyline();
            line1.Vertexes.Add(new LwPolylineVertex(v1));
            line1.Vertexes.Add(new LwPolylineVertex(v2));
            line1.Vertexes.Add(new LwPolylineVertex(v3));
            line1.Vertexes.Add(new LwPolylineVertex(v4));
            line1.Vertexes.Add(new LwPolylineVertex(v5));
            line1.Vertexes.Add(new LwPolylineVertex(v6));
            line1.Vertexes[4].Bulge = Math.Tan(-56 * pi / 180 / 4);
            fun.PolyFill(dxf, line1, 0.05, contour);

            //坐标
            Vector2 v7 = new Vector2(v6.X + 0.230 * L, v6.Y);
            Vector2 v8 = new Vector2(v7.X + l2, v7.Y);
            Vector2 v9 = new Vector2(v7.X, v7.Y + 0.563*a);
            Vector2 v10 = new Vector2(v8.X, v8.Y + 0.563*a);

            //作图
            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            

            //倒角
            var arc1 = Pus.Fillet4LwPline(line2, 2, r);
            var arc2 = Pus.Fillet4LwPline(arc1, 4, r);
            fun.PolyFill(dxf, arc2, 0.05, contour);







            //坐标
            Vector2 v11 = new Vector2(v10.X, v10.Y + 0.113 * d1);
            Vector2 v12 = new Vector2(v9.X, v9.Y + 0.113 * d1);
            Vector2 v13 = new Vector2(v11.X, v11.Y + 0.563*a);
            Vector2 v14 = new Vector2(v12.X, v12.Y + 0.563*a);

            //作图
            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v12));
            line3.Vertexes.Add(new LwPolylineVertex(v11));
            line3.Vertexes.Add(new LwPolylineVertex(v13));
            line3.Vertexes.Add(new LwPolylineVertex(v14));
            line3.Vertexes.Add(new LwPolylineVertex(v12));
          

            //倒角
            var arc3 = Pus.Fillet4LwPline(line3, 1, r);
            var arc4 = Pus.Fillet4LwPline(arc3, 3, r);
            fun.PolyFill(dxf, arc4, 0.05, contour);

            //坐标
            Vector2 v15 = new Vector2(v14.X - 0.230 * L, v14.Y);
            Vector2 v16 = new Vector2(v5.X, v5.Y + d1);
            Vector2 v17 = new Vector2(v4.X, v4.Y + d1);
            Vector2 v18 = new Vector2(v3.X, v3.Y + d);
            Vector2 v19 = new Vector2(v2.X, v2.Y + d);
            Vector2 v20 = new Vector2(v1.X, v1.Y + 0.877 * d);
            Vector2 v21 = new Vector2(v20.X, v20.Y - 0.133 * d);
            Vector2 v22 = new Vector2(v21.X + 0.41 * l,v21.Y);
            Vector2 v23 = new Vector2(v22.X, v22.Y - 0.137 * d);
            Vector2 v24 = new Vector2(v1.X, v1.Y + 0.579 * d);
            Vector2 v25 = new Vector2(v23.X, v23.Y - 0.295 * d);
            Vector2 v26 = new Vector2(v1.X, v1.Y + 0.28 * d);
            Vector2 v28 = new Vector2(v1.X , v1.Y+0.139*d);
            Vector2 v27 = new Vector2(v28.X + 0.413 * l, v28.Y);
            //作图
            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v14));
            line4.Vertexes.Add(new LwPolylineVertex(v15));
            line4.Vertexes.Add(new LwPolylineVertex(v16));
            line4.Vertexes.Add(new LwPolylineVertex(v17));
            line4.Vertexes.Add(new LwPolylineVertex(v18));
            line4.Vertexes.Add(new LwPolylineVertex(v19));
            line4.Vertexes.Add(new LwPolylineVertex(v20));
            line4.Vertexes.Add(new LwPolylineVertex(v21));
            line4.Vertexes.Add(new LwPolylineVertex(v22));
            line4.Vertexes.Add(new LwPolylineVertex(v23));
            line4.Vertexes.Add(new LwPolylineVertex(v24));
            line4.Vertexes.Add(new LwPolylineVertex(v26));
            line4.Vertexes.Add(new LwPolylineVertex(v25));
            line4.Vertexes.Add(new LwPolylineVertex(v27));
            line4.Vertexes.Add(new LwPolylineVertex(v28));
            line4.Vertexes[1].Bulge = Math.Tan(-56 * pi / 180 / 4);
            fun.PolyFill(dxf, line4, 0.05, contour);


            //坐标
            Vector2 v29 = new Vector2(v1.X, v1.Y + 0.173 * d);
            Vector2 v30 = new Vector2(v1.X, v1.Y + 0.625 * d);
            Vector2 v31 = new Vector2(v21.X + 0.279 * l,v21.Y);
            Vector2 v32 = new Vector2(v29.X + 0.255 * l, v29.Y + 0.125 * d);
            Vector2 v33 = new Vector2(v9.X + 0.88 * l2, v9.Y);
            Vector2 v34 = new Vector2(v33.X, v33.Y + 0.113*d1);

            //作图
            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v1));
            line5.Vertexes.Add(new LwPolylineVertex(v29));
            line5.Vertexes.Add(new LwPolylineVertex(v32));
            fun.PolyFill(dxf, line5, 0.05, contour);

            //作图
            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v26));
            line6.Vertexes.Add(new LwPolylineVertex(v30));
            line6.Vertexes.Add(new LwPolylineVertex(v31));
            fun.PolyFill(dxf, line6, 0.05, contour);

            //作图
            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v33));
            line7.Vertexes.Add(new LwPolylineVertex(v34));
  
            fun.PolyFill(dxf, line7, 0.05, contour);

            //坐标
            Vector2 v35 = new Vector2(v34.X + 2*l, v34.Y);
            Vector2 v36 = new Vector2(v35.X + 0.563 * a, v35.Y - 0.563 * a);
            Vector2 v37 = new Vector2(v36.X + 0.113 * d1, v36.Y);
            Vector2 v38 = new Vector2(v37.X + 0.563 * a, v37.Y + 0.563 * a);
            Vector2 v39 = new Vector2(v38.X, v38.Y + 0.113 * d1);
            Vector2 v40 = new Vector2(v39.X - 0.563 * a, v39.Y + 0.563 * a);
            Vector2 v41 = new Vector2(v40.X - 0.113 * d1, v40.Y);
            Vector2 v42 = new Vector2(v41.X - 0.563 * a, v41.Y - 0.563 * a);

            //作图
            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v35));
            line8.Vertexes.Add(new LwPolylineVertex(v36));
            line8.Vertexes.Add(new LwPolylineVertex(v37));
            line8.Vertexes.Add(new LwPolylineVertex(v38));
            line8.Vertexes.Add(new LwPolylineVertex(v39));
            line8.Vertexes.Add(new LwPolylineVertex(v40));
            line8.Vertexes.Add(new LwPolylineVertex(v41));
            line8.Vertexes.Add(new LwPolylineVertex(v42));
            line8.Vertexes.Add(new LwPolylineVertex(v35));
            fun.PolyFill(dxf, line8, 0.05, contour);


            //坐标

            Vector2 v43 = new Vector2(v2.X + 0.375 * l, v2.Y + 0.2586 * d);
            Vector2 v44 = new Vector2(v43.X, v43.Y + 0.483 * d);
            Vector2 v45 = new Vector2(v44.X + 0.135 * d, v44.Y - 0.06 * d);
            Vector2 v46 = new Vector2(v45.X, v45.Y - 0.363 * d);
            Vector2 v47 = new Vector2(v46.X + 0.225 * L, v46.Y);
            Vector2 v48 = new Vector2(v47.X, v47.Y + 0.363 * d);
            Vector2 v49 = new Vector2(v48.X + 0.156 * l, v48.Y - 0.1815 * d);
            Vector2 v50 = new Vector2(v5.X + 0.05015 * L, v5.Y);
            Vector2 v51 = new Vector2(v50.X, v50.Y + d1);
            Vector2 v52 = new Vector2(v49.X - 0.274 * l, v49.Y);
            Vector2 v53 = new Vector2(v1.X-0.2*l, v2.Y+0.5 * d);
            Vector2 v54 = new Vector2(v8.X+0.2*l, v8.Y + 0.5 * d1);
            Vector2 v55 = new Vector2(v8.X, v54.Y - 0.199 * d1);
            Vector2 v56 = new Vector2(v8.X, v54.Y + 0.199 * d1);
            Vector2 v57 = new Vector2(v35.X-0.1*d1, v35.Y + 0.05565 * d1);
            Vector2 v58 = new Vector2(v36.X + 0.05565 * d1, v36.Y-0.1*d1);
            Vector2 v59 = new Vector2(v38.X+0.1*d1, v38.Y + 0.05565 * d1);
            Vector2 v60 = new Vector2(v40.X - 0.05565 * d1, v40.Y+0.1*d1);


            //作图
            Line line9 = new Line(v27, v43);
            line9.Layer = refin;
            line9.Linetype = Linetype.Dashed;
            line9.LinetypeScale = 3;
            dxf.AddEntity(line9);

            Line line10 = new Line(v43, v44);
            line10.Layer = refin;
            line10.Linetype = Linetype.Dashed;
            line10.LinetypeScale = 3;
            dxf.AddEntity(line10);

            Line line11 = new Line(v44, v22);
            line11.Layer = refin;
            line11.Linetype = Linetype.Dashed;
            line11.LinetypeScale = 3;
            dxf.AddEntity(line11);

            Line line12 = new Line(v44, v45);
            line12.Layer = refin;
            line12.Linetype = Linetype.Dashed;
            line12.LinetypeScale = 3;
            dxf.AddEntity(line12);

            Line line13 = new Line(v45, v46);
            line13.Layer = refin;
            line13.Linetype = Linetype.Dashed;
            line13.LinetypeScale = 3;
            dxf.AddEntity(line13);

            Line line14 = new Line(v46, v47);
            line14.Layer = refin;
            line14.Linetype = Linetype.Dashed;
            line14.LinetypeScale = 3;
            dxf.AddEntity(line14);

            Line line15 = new Line(v47, v48);
            line15.Layer = refin;
            line15.Linetype = Linetype.Dashed;
            line15.LinetypeScale = 3;
            dxf.AddEntity(line15);

            Line line16 = new Line(v48, v49);
            line16.Layer = refin;
            line16.Linetype = Linetype.Dashed;
            line16.LinetypeScale = 3;
            dxf.AddEntity(line16);
           
            Line line17 = new Line(v47, v49);
            line17.Layer = refin;
            line17.Linetype = Linetype.Dashed;
            line17.LinetypeScale = 3;
            dxf.AddEntity(line17);

            Line line18 = new Line(v45, v48);
            line18.Layer = refin;
            line18.Linetype = Linetype.Dashed;
            line18.LinetypeScale = 3;
            dxf.AddEntity(line18);

            Line line19 = new Line(v43, v46);
            line19.Layer = refin;
            line19.Linetype = Linetype.Dashed;
            line19.LinetypeScale = 3;
            dxf.AddEntity(line19);

            Line line20 = new Line(v47, v5);
            line20.Layer = refin;
            line20.Linetype = Linetype.Dashed;
            line20.LinetypeScale = 3;
            dxf.AddEntity(line20);

            Line line21 = new Line(v49, v6);
            line21.Layer = refin;
            line21.Linetype = Linetype.Dashed;
            line21.LinetypeScale = 3;
            dxf.AddEntity(line21);

            Line line22 = new Line(v49, v15);
            line22.Layer = refin;
            line22.Linetype = Linetype.Dashed;
            line22.LinetypeScale = 3;
            dxf.AddEntity(line22);

            Line line23 = new Line(v48, v16);
            line23.Layer = refin;
            line23.Linetype = Linetype.Dashed;
            line23.LinetypeScale = 3;
            dxf.AddEntity(line23);


            Line line24 = new Line(v52, v51);
            line24.Layer = centre;
            line24.Linetype = Linetype.DashDot;
            line24.LinetypeScale = 3;
            dxf.AddEntity(line24);

            Line line25 = new Line(v52, v50);
            line25.Layer = centre;
            line25.Linetype = Linetype.DashDot;
            line25.LinetypeScale = 3;
            dxf.AddEntity(line25);

            Line line26 = new Line(v53, v54);
            line26.Layer = centre;
            line26.Linetype = Linetype.DashDot;
            line26.LinetypeScale = 3;
            dxf.AddEntity(line26);

            LwPolyline line27 = new LwPolyline();
            line27.Vertexes.Add(new LwPolylineVertex(v55));
            line27.Vertexes.Add(new LwPolylineVertex(v56));
            fun.PolyFill(dxf, line27, 0.05, contour);

            Line line28 = new Line(v57, v59);
            line28.Layer = centre;
            line28.Linetype = Linetype.DashDot;
            line28.LinetypeScale = 3;
            dxf.AddEntity(line28);


            Line line29 = new Line(v58, v60);
            line29.Layer = centre;
            line29.Linetype = Linetype.DashDot;
            line29.LinetypeScale = 3;
            dxf.AddEntity(line29);



            fun.dimrot1(dxf, v1, v3, -180, l * 1, 1, 1.2 , 1.6 , 0, "THL="+l, "", "");
            fun.dimrot1(dxf, v1, v2, 0, l * 1, 1, 1.2, 1.6, 0, "TCL="+l5+"P", "", "");
            fun.dimrot1(dxf, v1, v8, -180, l * 1.5, 1, 1.2, 1.6, 0, "OAL="+L, "", "");
            fun.dimrot1(dxf, v7, v8, -180, l * 1, 1, 1.2, 1.6, 0, "DRVL="+l2, "", "");


            fun.dimrot1(dxf, v2, v19, 90, l * 1, 1, 1.2, 1.6, 0, "TD="+d, "", "");
            fun.dimrot1(dxf, v7, v14, 90, l * 0.5, 1, 1.2, 1.6, 0, "DCONMS="+d1, "", "");
            fun.dimrot1(dxf, v36, v39, -135, l * 1, 1, 1.2, 1.6, 0, "DRVL="+a, "", "");
            fun.jdbz1(dxf, new Line(v23, fun.Polar(v23, 180 * pi / 180, 0.1)), new Line(v23, fun.Polar(v23, -170 * pi / 180, 0.1)), l * 2, "10°", 3);

            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v35));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v36));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v37));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v38));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v39));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v40));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v41));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v42));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v35));

            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int I = 0; I < hnumst1; I++)
            {
                htype1[I] = hatchpoly1.Explode()[I].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[I] == "Line")
                {
                    hline1[I] = (Line)hatchpoly1.Explode()[I];
                }
                if (htype1[I] == "Arc")
                {
                    harc1[I] = (Arc)hatchpoly1.Explode()[I];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);







            double scale = 1; //放大系数

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }




            //坐标


            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}